/**
 用到的listviewItem 是lnear_books_item.xml
 */
package org.buptsse.ui;

import java.util.LinkedList;
import java.util.List;

import org.buptsse.entity.FilterResultItem;
import org.buptsse.thread.FindSellByFilterThread;
import org.buptsse.utils.APPConstant;
import org.buptsse.utils.GloableInstance;
import org.buptsse.utils.ImageService;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class SearchResultActivity extends ListActivity implements OnScrollListener,OnClickListener{

	ListAdapter listAdapter = new ListAdapter();
	ListView list;
	int visibleItemCount;
	
	int scrollState;
	LinkedList<FilterResultItem> resultList = new LinkedList<FilterResultItem>();
	int lastItem;
	int count;

	SharedPreferences preferences = null;
	private Button backButton,refreshButton;
	private TextView titleTextView;
	private String sellerSchool,condition,keyword;
	private int price;
	private ProgressDialog pDialog;

	Handler myHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case APPConstant.NetUnavailable:
				Toast.makeText(getApplicationContext(), "网络连接不可用", 1000).show();
				break;
			case APPConstant.findSellByFilterError:
				Toast.makeText(getApplicationContext(), "没有找到符合条件的结果", 1000).show();
				listAdapter.count=0;
				listAdapter.notifyDataSetChanged();
				break;
			case APPConstant.findSellByFilterFinish:
				resultList=(LinkedList<FilterResultItem>) msg.obj;
				listAdapter.count=resultList.size();
				listAdapter.notifyDataSetChanged();
				break;
			}
			super.handleMessage(msg);
			pDialog.dismiss();
		}
	};

	@SuppressWarnings("unchecked")
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Intent it = getIntent();
		this.sellerSchool=it.getStringExtra("sellerSchool");
		this.condition=it.getStringExtra("condition");
		this.keyword=it.getStringExtra("keyword");
		this.price=it.getIntExtra("price", -1);
		setContentView(R.layout.lsearch_result);
		listAdapter.count=0;
		this.setListAdapter(listAdapter);
		backButton=(Button) findViewById(R.id.rd_back);
		backButton.setOnClickListener(this);
		refreshButton=(Button) findViewById(R.id.rd_refresh);
		refreshButton.setOnClickListener(this);
		titleTextView=(TextView) findViewById(R.id.title_textview);
		titleTextView.setOnClickListener(this);
		findMatchedSells();
	}

	//list item被点击的响应函数
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		Intent intent=new Intent();
		intent.putExtra("sellid", resultList.get(position).getSellid());
		intent.setClass(SearchResultActivity.this, OneBookActivity.class);
		startActivity(intent);
	}

	private void findMatchedSells()
	{
		FindSellByFilterThread fsbft=new FindSellByFilterThread(getApplicationContext(), myHandler, GloableInstance.user.getName(), sellerSchool, condition, keyword, price);
		fsbft.start();
		ShowWaittingDialog("正在查询");
	}
	
	public void ShowWaittingDialog(String msg)
	{
		pDialog=new ProgressDialog(this);
		pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        pDialog.setMessage(msg); 
        pDialog.show(); 
	}
	
	class ListAdapter extends BaseAdapter {
		int count = 10;
		@Override
		public int getCount() {
			return count;
		}
		@Override
		public Object getItem(int position) {
			return position;
		}
		@Override
		public long getItemId(int position) {
			return position;
		}
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			LayoutInflater inflater = LayoutInflater.from(SearchResultActivity.this);
			View view = inflater.inflate(R.layout.filter_result_list_item, null);
			TextView bookNameTextView=(TextView) view.findViewById(R.id.bookname);
			TextView priceTextView=(TextView) view.findViewById(R.id.price);
			TextView sellerTextView=(TextView) view.findViewById(R.id.seller_name);
			TextView schoolTextView=(TextView) view.findViewById(R.id.school);
			TextView beginTimeTextView=(TextView) view.findViewById(R.id.begintime);
			ImageView bookPhoto=(ImageView) view.findViewById(R.id.bookimage);
			FilterResultItem item=resultList.get(position);
			bookPhoto.setImageBitmap(ImageService.getImageByUrl(item.getBookimage()));
			bookNameTextView.setText(item.getBookname());
			priceTextView.setText(item.getPrice());
			sellerTextView.setText(item.getSeller());
			schoolTextView.setText(item.getSellerschool());
			beginTimeTextView.setText(item.getBegintime());
			return view;
		}
	}
	
	
	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		this.visibleItemCount = visibleItemCount;
		lastItem = firstVisibleItem + visibleItemCount - 1;
		if (listAdapter.count >= count) {
			list.removeFooterView(view);
		}
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		this.scrollState = scrollState;
	}

	@Override
	public void onClick(View v) {
		int vid=v.getId();
		System.out.println("click"+vid);
		switch(vid){
		case R.id.rd_back:
			finish();
			break;
		case R.id.rd_refresh:
			findMatchedSells();
			break;
		case R.id.title_textview:
			//重新设置搜索条件
			Intent intent=new Intent();
			intent.setClass(SearchResultActivity.this, SearchActivity.class);
			startActivity(intent);
			finish();
			break;
		}
	}

}
